package com.xiaobug.demo.alipay.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;


import com.xiaobug.demo.R;
import com.xiaobug.demo.BR;
import com.xiaobug.demo.alipay.MenuItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单grid列表Adapter
 * Created by ljb on 2018/5/17.
 */

public class MenuRecyclerviewGridAdapter extends RecyclerView.Adapter<BindingViewHolder> {
    private List<MenuItem> mData;
    private final LayoutInflater mInflater;

    public MenuRecyclerviewGridAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BindingViewHolder<>(DataBindingUtil.inflate(mInflater, R.layout.alipay_rv_item, parent, false));
    }

    @Override
    public void onBindViewHolder(BindingViewHolder holder, int position) {
        MenuItem menuItem = mData.get(position);
        holder.getBinding().setVariable(BR.item, menuItem);

    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void setData(List<MenuItem> menuItemList) {
        if (mData == null)
            mData = new ArrayList<>();
        mData.clear();
        mData.addAll(menuItemList);
    }

    public List<MenuItem> getData() {
        return mData;
    }
}
